import 'package:json_annotation/json_annotation.dart';
import 'package:terminal_flutter_demo/models/address.dart';

part 'user.g.dart';

// 当你首次创建 json_serializable 类时
// 运行代码生成器来生成序列化数据模板flutter pub run build_runner watch
@JsonSerializable(explicitToJson: true)
class User {
  User(this.name, this.email);

  String name;

  String email;

  late Address address;

  /// 如果 API 返回带有 蛇形命名方式 的对象，并且你想要在你的模型里使用 小驼峰 的命名方式，你可以使用带有一个 name 参数的 @JsonKey 注解
  @JsonKey(name: 'registration_date_millis')
  late final int registrationDateMillis;

  /// 服务端的数据有时无法确认，所以在客户端很有必要进行数据校验和保护。其他常见的 @JsonKey 声明方法包括
  /// Tell json_serializable to use "defaultValue" if the JSON doesn't
  /// contain this key or if the value is `null`.
  @JsonKey(defaultValue: false)
  late final bool isAdult;

  /// When `true` tell json_serializable that JSON must contain the key,
  /// If the key doesn't exist, an exception is thrown.
  @JsonKey(required: true)
  late final String id;

  /// When `true` tell json_serializable that generated code should
  /// ignore this field completely.
  @JsonKey(ignore: true)
  late final String verificationCode;

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}
